<#include "../include/header.html">
<body>
<script type="text/javascript">

    function copyText(obj)
    {
        var transfer = document.getElementById('J_CopyTransfer');
        if (!transfer) {
            transfer = document.createElement('textarea');
            transfer.id = 'J_CopyTransfer';
            transfer.style.position = 'absolute';
            transfer.style.left = '-9999px';
            transfer.style.top = '-9999px';
            document.body.appendChild(transfer);
        }
        transfer.value = $(obj).html();
        transfer.focus();
        transfer.select();
        document.execCommand('Copy', false, null);

    }

    var viewModel = Hap.createGridViewModel("#grid",{
    });

</script>
<div id="dialog"></div>
<div id="design"></div>
<div id="preview"></div>
<div id="param"></div>
<div id="page-content">
    <div class="pull-left" id="toolbar-btn" style="padding-bottom:10px;">
        <span class="btn btn-primary" style="float:left;margin-right:5px;"  data-bind="click:create"><i class="fa fa-plus-square" style="margin-right:3px;"></i><@spring.message "hap.new"/></span>
        <span class="btn btn-success k-grid-save-changes" style="float:left;margin-right:5px;" data-bind="click:save"><i class="fa fa-save" style="margin-right:3px;"></i><@spring.message "hap.save"/></span>
        <span  class="btn btn-danger" style="float:left;" data-bind="click:remove"><i class="fa fa-trash-o" style="margin-right:3px;"></i><@spring.message "hap.delete"/></span>
    </div>
    <script>kendo.bind($('#toolbar-btn'), viewModel);</script>
    <div class="pull-right" id="query-form" style="padding-bottom:10px;">
        <input data-role="maskedtextbox" placeholder='<@spring.message "report.reportcode"/>' type="text" style="width: 150px;float:left;margin-right:5px;" data-bind="value:model.reportCode" class="k-textbox">
        <input data-role="maskedtextbox" placeholder='<@spring.message "report.name"/>' type="text" style="width: 150px;float:left;margin-right:5px;" data-bind="value:model.name"
               class="k-textbox">
        <span class="btn btn-primary" data-bind="click:query" style="width: 80px;float:left;margin-right:5px;" type="submit"><i class="fa fa-search" style="margin-right:3px;"></i><@spring.message "hap.query"/></span>
        <div style="clear:both"></div>
    </div>
    <script>kendo.bind($('#query-form'), viewModel);</script>
    <div style="clear: both;">
        <div id="grid"></div>
    </div>
</div>
<script type="text/javascript">

    Hap.initEnterQuery("#query-form",viewModel.query);

    var dataSource = Hap.createGridDataSource({
        url: '${base.contextPath}/sys/report/',
        model: {
            id: "reportId",
            fields: {
                reportCode: {validation: {required: true}},
                name: {validation: {required: true}},
                fileId: {validation: {required: true}},
                defaultQuery: {defaultValue: 'Y', type: 'boolean', checkedValue: 'Y', uncheckedValue: 'N'}
            },
            editable: function (col) {
                if ( (col == "reportCode" || col == "fileId") && this.id) {
                    return false;
                }
                return true;
            }
        }
    });
    var grid = $("#grid").kendoGrid({
        dataSource: dataSource,
        navigatable: true,
        sortable: true,
        dataBound: function(){
            if(parent.autoResizeIframe){
                parent.autoResizeIframe('${RequestParameters.functionCode!}')
            }
            $('[data-toggle="tooltiptitle"]').kendoTooltip({
                position:"top",
                content:"复制以下url以用于资源管理挂载报表",
                animation: {
                    open: {
                        duration: 1000
                    },
                    close: {
                        duration: 1000
                    }
                }
            });
            $('[name="title"]').kendoTooltip({
                showOn: "click",
                position:"right",
                content:"已复制！",
                animation: {
                    open: {
                        duration: 1000
                    },
                    close: {
                        duration: 1000
                    }
                }
            });
            var tip=$("#grid").kendoTooltip({
                filter:'[name="title"]',
                position:"right",
                content:"单击即可复制",
                animation: {
                    open: {
                        duration: 1000
                    }
                }
            }).data("kendoTooltip");
            $('[name="title"]').click(function () {
                tip.hide()
            })
        },
        resizable: true,
        scrollable: true,
        editable: true,
        selectable:'multiple,rowbox',
        pageable: {
            pageSizes: [5, 10, 20, 50],
            refresh: true,
            buttonCount: 5
        },
        columns: [
            {
                field: "reportCode",
                title: '<@spring.message "report.reportcode"/>',
                width: 80
            },{
                field: "fileId",
                title: '<@spring.message "report.fileid"/>',
                width: 100,
                sortable: false,
                template:function(dataItem){
                    return dataItem['fileName'] || "";
                },
                editor: function (container, options) {
                    $('<input required name="' + options.field + '"/>')
                        .appendTo(container)
                        .kendoLov($.extend(<@lov "LOV_REPORT_FILE"/>,{
                        textField: 'fileName',
                        model: options.model,
                        change:function () {
                            options.model.set("sourceType",this._dataItem.sourceType);

                        }
                    }));
                }
            }, {
                field: "name",
                title: '<@spring.message "report.name"/>',
                width:  80,
                sortable: false,
            }, {
                field: "description",
                title: '<@spring.message "report.description"/>',
                width: 100,
                sortable: false,
            }, {
                field: "defaultQuery",
                title: '<@spring.message "report.defaultquery"/>',
                width: 55,
                headerAttributes: {
                    "class": "table-header-cell",
                    style: "text-align: center"
                },
                attributes: {
                    style: "text-align:center"
                },
                sortable: false,
            }, {
                width: 160,
                headerAttributes: {
                    "class": "table-header-cell",
                    style: "text-align: center",
                },
                headerTemplate: kendo.template('<@spring.message "report.url"/>' +
                    '<span data-toggle="tooltiptitle" data-placement="top" style="color:orange" ' +
                    'class="glyphicon glyphicon-question-sign"></span>'),
                template:function(dataItem){
                    if(!!dataItem.id){
                        return '<span name="title" data-toggle="tooltip" data-placement="right" style="background-color:lightgrey;border-radius:3px;color:mediumvioletred;font-family:Consolas,Liberation Mono,Menlo,Courier,monospace " onclick="copyText(this)">rpt/preview.html?code='+dataItem.reportCode+'</span>'
                    }else{
                        return '';
                    }
                }
            },
            {
                title:'<@spring.message "report.parameter"/>',
                width : 55,
                headerAttributes: {
                    "class": "table-header-cell",
                    style: "text-align: center"
                },
                attributes: {
                    style: "text-align:center"
                },
                template:function(dataItem){
                    if(!!dataItem.id){
                        return '<a href="javascript:void(0)" onclick=setParam(\"'+dataItem.id+'\",\"'+dataItem.reportCode+'\",\"'+dataItem.sourceType+'\")><@spring.message "hap.edit"/></a>'
                    }else{
                        return '';
                    }
                },
            },{
                title:'<@spring.message "report.design"/>',
                width : 55,
                headerAttributes: {
                    "class": "table-header-cell",
                    style: "text-align: center"
                },
                attributes: {style: "text-align:center"},
                template:function(dataItem){
                    if(!!dataItem.id){
                        return '<a href="javascript:void(0)" onclick=design(\"'+dataItem.fileName+'\")><@spring.message "report.design"/></a>'
                    }else{
                        return '';
                    }
                },
            },{
                title:'<@spring.message "report.preview"/>',
                width : 50,
                headerAttributes: {
                    "class": "table-header-cell",
                    style: "text-align: center"
                },
                attributes: {
                    style: "text-align:center"
                },
                template:function(dataItem){
                    if(!!dataItem.id){
                        return '<a href="javascript:void(0)" onclick=preview(\"'+dataItem.reportCode+'\")><@spring.message "report.preview"/></a>'
                    }else{
                        return '';
                    }
                },
            },
        ]
    }).data("kendoGrid");
    Hap.autoResizeGrid("#grid");

     function setParam(id,code,sourceType){
         var  url =  '${base.contextPath}/sys/sys_parameter_config.html?code=REPORT&targetId=' + id+"&reportCode="+code+"&sourceType="+sourceType;
         var param = $("#param").kendoWindow({
             width: 1100,
             height: 650,
             title: '<@spring.message "hap.edit"/>',
             visible: false,
             iframe: true,
             resizable:false,
             modal: true,
             content:url
         }).data("kendoWindow");
         if(parent.autoResizeIframe){
             parent.autoResizeIframe('${RequestParameters.functionCode!}', 750, function(){
                 var win = $("#param").data("kendoWindow");
                 win.center().open();
             })
         }else {
             var win = $("#param").data("kendoWindow");
             win.center().open();
         }
     }
    function design(fileName) {
        var design = $("#design").kendoWindow({
            title: '<@spring.message "report.design"/>',
            visible: false,
            iframe: true,
            resizable:false,
            modal: true,
            draggable: false,
            content: '${base.contextPath}/ureport/designer?_u=database:'+fileName,
        }).data("kendoWindow");
        design.maximize().open();
    }
    function preview(reportCode) {
        var preview = $("#preview").kendoWindow({
            title: '<@spring.message "report.preview"/>',
            visible: false,
            iframe: true,
            resizable:false,
            modal: true,
            draggable: false,
            content: 'preview.html?code='+reportCode,
        }).data("kendoWindow");
        preview.maximize().open();
    }
    var functionCode = '${RequestParameters.functionCode!}';
</script>
</body>
</html>
